package com.flame.schedule.controller;

import com.flame.auth.core.annotation.HasAuth;
import com.flame.core.protocol.PageResult;
import com.flame.core.protocol.Result;
import com.flame.schedule.controller.vo.JobInfoVo;
import com.flame.schedule.entity.JobInfo;
import com.flame.schedule.service.JobInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * flame_schedule_job_info 任务信息表
 *
 * @author 吴欣童
 * @since 2022-12-03 19:56:44
 */
@RestController
public class JobInfoController {

    @Autowired
    private JobInfoService jobInfoService;

    @HasAuth("job-schedule:manual")
    @PostMapping("/schedule/invoke")
    public Result invoke(@RequestBody JobInfo jobInfo) {
        jobInfoService.invoke(jobInfo);
        return Result.success();
    }

    @HasAuth("job-schedule:start")
    @PostMapping("/schedule/pause")
    public Result pauseJob(@RequestBody JobInfo jobInfo) {
        jobInfoService.pausuJob(jobInfo);
        return Result.success();
    }

    @HasAuth("job-schedule:start")
    @PostMapping("/schedule/resume")
    public Result resumeJob(@RequestBody JobInfo jobInfo) {
        jobInfoService.resumeJob(jobInfo);
        return Result.success();
    }

    @HasAuth("job-schedule")
    @PostMapping("/schedule/group")
    public Result jobGroupList() {
        return Result.success(jobInfoService.jobGroupList());
    }

    @HasAuth("job-schedule")
    @PostMapping("/schedule/page")
    public PageResult pageJobInfo(@RequestBody JobInfoVo jobInfoVo) {
        return PageResult.success(jobInfoService.pageJobInfo(jobInfoVo));
    }

    @HasAuth("job-schedule:add")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/schedule/add")
    public Result addJobInfo(@RequestBody @Validated JobInfo jobInfo) {
        jobInfoService.addJobInfo(jobInfo);
        return Result.success();
    }

    @HasAuth("job-schedule:edit")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/schedule/update")
    public Result updateJobInfo(@RequestBody @Validated JobInfo jobInfo) {
        jobInfoService.updateJobInfo(jobInfo);
        return Result.success();
    }

    @HasAuth("job-schedule:delete")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/schedule/delete")
    public Result deleteJobInfo(@RequestBody @Validated JobInfo jobInfo) {
        jobInfoService.deleteJobInfo(jobInfo);
        return Result.success();
    }
}
